import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ApiService } from '../api.service';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ScrewService extends ApiService {
  constructor(http: HttpClient) {
    super(http);
  }

  // Sends request to transfer `amnt` screws from `corpAName` to `corpBName`.
  transfer(corpAName: string, corpBName: string, amnt: number): Observable<any> {
    const form = new FormData();
    form.append('corpAName', corpAName);
    form.append('corpBName', corpBName);
    form.append('amnt', amnt === null ? '' : amnt.toString());

    return this.http.post<any>(
      super.prefixUrlWithBase('/screw/transfer'), form);
  }

  // Sends request to count the number of screws in `corpName`.
  count(corpName: string): Observable<number> {
    return this.http.get<number>(
      super.prefixUrlWithBase(`/screw/count?corpName=${corpName}`)
    );
  }
}
